//
//  UIECGPreview.m
//  vhECGTools
//
//  Created by jia yu on 2021/11/23.
//

#import "UIECGPreview.h"
#import "vhECGPath.h"
#import <ZMFoundation/ZMFoundation.h>
@interface UIECGPreview(){
    NSMutableArray *ecgsArray;
    CAShapeLayer *bg1mmLayer;
    CAShapeLayer *bg5mmLayer;
    CAShapeLayer *bg10mmLayer;
    CAShapeLayer *ecgLayer;
}
@end

@implementation UIECGPreview
@synthesize settings;

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){        
        NSString *file = [NSBundle ex_pathForResource:@"demo" type:@"plist" bundleName:@"vhECGTools"];
        NSArray *sArray = [[NSArray alloc] initWithContentsOfFile:file].firstObject;
        ecgsArray  = [[NSMutableArray alloc] init];
        for (int i=0;i<5;i++){
            [ecgsArray addObjectsFromArray:sArray];
        }
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    self.backgroundColor = [settings ecgBackgroundColor];
    float pixPermm = [settings pixPermm];
    
    if (bg1mmLayer){
        [bg1mmLayer removeFromSuperlayer];
    }
    if (bg5mmLayer){
        [bg5mmLayer removeFromSuperlayer];
    }
    if (bg10mmLayer){
        [bg10mmLayer removeFromSuperlayer];
    }
    if (ecgLayer){
        [ecgLayer removeFromSuperlayer];
    }
    CGMutablePathRef path = [vhECGPath buildBg_mmLinesIn:CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height) pix:pixPermm];
    bg1mmLayer = [CAShapeLayer layer];
    bg1mmLayer.path = path;
    bg1mmLayer.lineWidth = [settings bg1mmLineWidth];
    bg1mmLayer.strokeColor = [settings bg1mmLineColor].CGColor;
    bg1mmLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:bg1mmLayer];
    CGPathRelease(path);
    
    
    path = [vhECGPath buildBg_5mmLinesIn:CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height) pix:pixPermm];
    bg5mmLayer = [CAShapeLayer layer];
    bg5mmLayer.path = path;
    bg5mmLayer.lineWidth = [settings bg5mmLineWidth];
    bg5mmLayer.strokeColor = [settings bg5mmLineColor].CGColor;
    bg5mmLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:bg5mmLayer];
    CGPathRelease(path);

    path = [vhECGPath buildBg_10mmLinesIn:CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height) pix:pixPermm];
    bg10mmLayer = [CAShapeLayer layer];
    bg10mmLayer.path = path;
    bg10mmLayer.lineWidth = [settings bg10mmLineWidth];
    bg10mmLayer.strokeColor = [settings bg10mmLineColor].CGColor;
    bg10mmLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:bg10mmLayer];
    CGPathRelease(path);
    
    
    float middel = self.frame.size.height * 0.66;
    float seekX = 25.f * pixPermm/440.f;
    path = [vhECGPath buildOneLineECG:ecgsArray zeroX:0.f pointPerPoint:seekX middleY:middel rateY:10.f pix:pixPermm];
    ecgLayer = [CAShapeLayer layer];
    ecgLayer.path = path;
    ecgLayer.lineWidth = [settings ecgLineWidth];
    ecgLayer.strokeColor = [settings ecgColor].CGColor;
    ecgLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:ecgLayer];
    CGPathRelease(path);
}

@end
